/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
 *
 * FILENAME:
 * command.h
 *
 * PURPOSE:
 *  This code implements the command data structure between tasks on
 *  the flight computer and from ground control station
 *
 * CREATED:
 * 11/2000 by Cory Dixon
 *
 * LAST MODIFIED:
 * $Author: dixonc $
 * $Date: 2005/07/26 20:34:24 $
 * $Revision: 1.5 $
 *
 ***********************************************************************/

#ifndef _ZIGBEE_H
#define _ZIGBEE_H

#include "commandStruct.h"
#include "definedCommands.h"
#include "type_defs.h"

#include <iostream>
#include <vector>

using namespace std;

// Define a template class for a vector of IDs.
typedef vector<uint8_t> CMDVECTOR;

/***********************************************************************
 * This struct is an array of commands that is used to
 * be the defined commands in the system. The maximum length
 * of the array is set by CMD_MAX_NUM
 *
 * id number can never be 255 - implies non-defined command
 * numParams can never be 255 - implies dynamic size
 ***********************************************************************/

//
// Maximum number of parameters is 252 (which is more than enough) 
//  and total packet size should be an even number, though the code
//  is written to handle odd size packets
//

#define API_DELIMETER_SIZE 1
#define API_LENGTH_SIZE 2
#define API_ID_SIZE 1
#define API_FRAME_SIZE 1
#define API_ADDRESS_SIZE 2
#define API_RSSI_SIZE 1
#define API_OPTIONS_SIZE 1
#define API_CSUM_SIZE 1               //checksum size
#define API_NUM(buf)                         ( buf[2] )
#define API_SIZE(buf)                          ( API_NUM(buf) + API_DELIMETER_SIZE +  API_LENGTH_SIZE + API_CSUM_SIZE)
#define API_CSUM(buf) 		(*((uint8_t *)(buf + API_NUM(buf) + API_DELIMETER_SIZE +  API_LENGTH_SIZE )))
#define API_TX_HEADER_SIZE          ( API_DELIMETER_SIZE + API_LENGTH_SIZE + API_ID_SIZE + API_FRAME_SIZE + API_ADDRESS_SIZE + API_OPTIONS_SIZE )


class ZigbeeQueue
{

public:
	ZigbeeQueue() { ; }
	~ZigbeeQueue() {;}

	void popCommand();

	// append to the queue, a buffer of length num
	void push(uint8_t * buf, size_t num) ;

	// pop a single byte off of the queue
	uint8_t popByte() ;

	// clears the contents
	void clear() {
		return zigbee_fifo.clear();
	}

	uint16_t getMavNum();
	void requestAddress(uint8_t * buf);

	// checks to see if the next set of bytes on the byte
	// queue form a valid command

	bool APIisValid() const ;

	bool APIneedMoreBytes() const;

	void APIsetCheckSum(uint8_t message[], uint8_t size);

	uint8_t getByte(uint8_t j);

	uint16_t size();
	
	void APIheaders(uint8_t array[], uint16_t destination, uint8_t size);

private:

	//CMDVECTOR fifo;
	CMDVECTOR zigbee_fifo;

	// private functions
	uint8_t APIcalcCheckSum() const;
	uint8_t APIcalcTXCheckSum(uint8_t message[], uint8_t size) const;
	//void APIsetCheckSum(uint8_t message[], uint8_t size);
};

#endif // _ZIGBEE_H
